
package com.benimetum.beans;

import com.benimetum.items.*;
import com.benimetum.mail.MailSender;
import com.benimetum.managers.*;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.DateSelectEvent;
import org.primefaces.event.ScheduleEntrySelectEvent;
import org.primefaces.model.DefaultScheduleEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.ScheduleEvent;
import org.primefaces.model.ScheduleModel;

/**
 *
 * @author Ethem Cem Ozkan
 * @email ethemcem.ozkan@gmail.com
 * No 07110604
 */
@ManagedBean(name="CourseBean")
@SessionScoped
public class CourseBean implements Serializable
{
    private Course target;
    private ScheduleModel eventModel;  
    private UploadedFile[] files;
    private org.primefaces.model.UploadedFile upload;
    private Announcement[] anns;
    private String text;
    private String textEvent;
    private String textMail;
    private String info; 
    private String header;
    private Date date;
    private ScheduleEvent event = new DefaultScheduleEvent();  
    private Result[] result;
    private String examName;

    public String getExamName() {
        return examName;
    }

    public void setExamName(String examName) {
        this.examName = examName;
    }

    public Result[] getResult() {
        return result;
    }

    public void setResult(Result[] result) {
        this.result = result;
    }
    
    
    public Course getTarget() {
	  return target;
    }

    public String getTextMail() {
	  return textMail;
    }

    public String getTextEvent() {
	  return textEvent;
    }

    public Announcement[] getAnns() {
	  return anns;
    }

    public UploadedFile[] getFiles() {
	  return files;
    }

    public String getHeader() {
	  return header;
    }

    public String getInfo() {
	  return info;
    }

    public org.primefaces.model.UploadedFile getUpload() {
	  return upload;
    }

    public void setText(String text) {
	  this.text = text;
    }

    public void setFiles(UploadedFile[] files) {
	  this.files = files;
    }

    public void setTextMail(String textMail) {
	  this.textMail = textMail;
    }

    public void setTextEvent(String textEvent) {
	  this.textEvent = textEvent;
    }

    public void setHeader(String header) {
	  this.header = header;
    }

    public void setDate(Date date) {
	  this.date = date;
    }

    public void setAnns(Announcement[] anns) {
	  this.anns = anns;
    }

    public void setUpload(org.primefaces.model.UploadedFile upload) {
	  this.upload = upload;
    }

    public void setInfo(String info) {
	  this.info = info;
    }

    public String getText() {
	  return text;
    }

    public Date getDate() {
	  return date;
    }

    public void setTarget(Course target) {
	  this.target = target;
          result=new Result[target.getSTUDENTS().length];
          for (int i = 0; i < result.length; i++) {
            result[i]=new Result();
            result[i].setStudent(target.getSTUDENTS()[i]);
            result[i].setResult(""); 
          }
	  files=FileManager.getCoursesFiles(target.getID());
	  anns=AnnouncementManager.getCoursesAnnouncements(target.getID());
	  Event[] studentsEvents = AnnouncementManager.getCoursesEvents(target.getID());
	  eventModel=new DefaultScheduleModel();
	  for (int i = 0; i < studentsEvents.length; i++) {
		Event e = studentsEvents[i];
		DefaultScheduleEvent ev = new DefaultScheduleEvent(e.getText(),e.getDate(), e.getDate());
		ev.setId(e.getId());
		eventModel.addEvent(ev);
	  }
    }

    public void setEvent(ScheduleEvent event) {
	  this.event = event;
    }

    public ScheduleEvent getEvent() {
	  return event;
    }
    
    public boolean coursesTeacher(){
	  return target.getTEACHER().getId().equals(LoginManager.getUser().getId());
    }

    public ScheduleModel getEventModel() {
	  return eventModel;
    }

    public void setEventModel(ScheduleModel eventModel) {
	  this.eventModel = eventModel;
    }
    
    public void addEvent(){
	  AnnouncementManager.createCourseEvent(textEvent, date, target.getSTUDENTS(), target.getTEACHER().getId(), target.getID());
	  FacesContext context = FacesContext.getCurrentInstance();
	  HomePageBean obj = context.getApplication().evaluateExpressionGet(context, "#{HomePageBean}", HomePageBean.class);
	  obj.events();
	  Event[] studentsEvents = AnnouncementManager.getCoursesEvents(target.getID());
	  eventModel=new DefaultScheduleModel();
	  for (int i = 0; i < studentsEvents.length; i++) {
		Event e = studentsEvents[i];
		DefaultScheduleEvent ev = new DefaultScheduleEvent(e.getText(),e.getDate(), e.getDate());
		ev.setId(e.getId());
		eventModel.addEvent(ev);
	  }
	  try {
		FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
	  } catch (IOException ex) {
		Logger.getLogger(CourseBean.class.getName()).log(Level.SEVERE, null, ex);
	  }
    }
    
    public void addEvent2(){
	  AnnouncementManager.createCourseEvent(event.getTitle(), event.getEndDate(), target.getSTUDENTS(), target.getTEACHER().getId(), target.getID());
	  FacesContext context = FacesContext.getCurrentInstance();
	  HomePageBean obj = context.getApplication().evaluateExpressionGet(context, "#{HomePageBean}", HomePageBean.class);
	  obj.makeBoxFalse();
	  try {
		FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
	  } catch (IOException ex) {
		Logger.getLogger(CourseBean.class.getName()).log(Level.SEVERE, null, ex);
	  }
    }
    
    public Homework[] getCourseHomework(){
	  return HomeworkManager.getCoursesHomework(target.getID());
    }
    
    public Forum courseForum(){
	  return ForumManager.getCoursesForum(target.getID());
    }
    
    public void addAnnouncement(){
	  AnnouncementManager.createCourseAnnouncement(text+"<br>Eklenme Tarihi: "+new Date().toGMTString(), target.getSTUDENTS(), target.getTEACHER().getId(), target.getID());
	  FacesContext context = FacesContext.getCurrentInstance();
	  HomePageBean obj = context.getApplication().evaluateExpressionGet(context, "#{HomePageBean}", HomePageBean.class);
	  obj.makeBoxFalse();
	  anns=AnnouncementManager.getCoursesAnnouncements(target.getID());
	  try {
		FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
	  } catch (IOException ex) {
		Logger.getLogger(CourseBean.class.getName()).log(Level.SEVERE, null, ex);
	  }
    }
    
    public void sendEmail(){
	  Student[] student = target.getSTUDENTS();
	  String[] email=new String[student.length];
	  for (int i = 0; i < student.length; i++) {
		email[i]=student[i].getEmail();
		
	  }
	  MailSender obj=new MailSender(email, "BenimEtum", header, textMail);
	  Thread thread=new Thread(obj);
	  thread.start();
	  FacesContext context = FacesContext.getCurrentInstance();
	  HomePageBean bean = context.getApplication().evaluateExpressionGet(context, "#{HomePageBean}", HomePageBean.class);
	  bean.makeBoxFalse();
	  try {
		FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
	  } catch (IOException ex) {
		Logger.getLogger(CourseBean.class.getName()).log(Level.SEVERE, null, ex);
	  }
    }
    
    public void addFile(){
	  File f = FileManager.convertUploadedFileToFile(upload);
	  FileManager.addFile(f, info, target.getID());
	  files=FileManager.getCoursesFiles(target.getID());
    }
    
    public void onEventSelect(ScheduleEntrySelectEvent selectEvent) {  
        event = selectEvent.getScheduleEvent();  
    }  
    
    public void onDateSelect(DateSelectEvent selectEvent) {  
        event = new DefaultScheduleEvent(Math.random() + "", selectEvent.getDate(), selectEvent.getDate());  
    }  
    
    public void updateEvent(){
	  AnnouncementManager.updateEvent(event.getTitle(), event.getId(),event.getEndDate());
    }
    
    public boolean checkTarget(){
	  return target!=null;
    }
    
    public void announceExam(){
        Person[] person=new Person[1];
        for (int i = 0; i < result.length; i++) {
            person[0]=result[i].getStudent();
            AnnouncementManager.createAnnouncement("[SINAV] "+target.getNAME()+" dersi "+examName+" Sonucu: "+"<br>"+result[i].getStudent().getFullName()+" :"+result[i].getResult(),person, LoginManager.getUser().getId());
            
        }
    }
}
